package com.innovation.chengzhangriji.util;

import android.text.TextUtils;

import com.innovation.chengzhangriji.Baby;
import com.innovation.chengzhangriji.http.Client;

import org.joda.time.DateTime;
import org.joda.time.Period;
import org.joda.time.PeriodType;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class BabyUtil {

    public static String getDateToString(long time) {
        DateTime dateTime = new DateTime(time);
        dateTime = new DateTime(dateTime.year().get(), dateTime.monthOfYear().get(), dateTime.dayOfMonth().get(), 0, 0);
        DateTime today = new DateTime(System.currentTimeMillis());
        Period d = new Period(dateTime, today, PeriodType.days());
        if (d.getDays() == 0) {
            return "今天";
        } else if (d.getDays() == 1) {
            return "昨天";
        }
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd", Locale.SIMPLIFIED_CHINESE);
        Date date = new Date(time);
        return sf.format(date);
    }

    public static String getTimeToString(long time) {
        SimpleDateFormat sf = new SimpleDateFormat("HH:mm", Locale.SIMPLIFIED_CHINESE);
        Date date = new Date(time);
        return sf.format(date);
    }

//    public static String getCount(int state, long time) {
//        return getBabyDay(mBaby, state, time);
//    }

    public static String getBabyDay(Baby baby, int state, long time) {
        switch (state) {
            case Client.STATE_READY:
                return "备孕中";
            case Client.STATE_START:
                if (baby.getDueDate() != 0) {
                    return getBabyDayByDueDateOrLastDate(baby.getDueDate() - 280 * 24 * 60 * 60 * 1000l, time);
                } else {
                    return getBabyDayByDueDateOrLastDate(baby.getLastDate(), time);
                }
            case Client.STATE_END:
                return getBabyDayByBirthday(baby.getBirthday(), time);
        }
        return "";
    }

    public static String getBabyDay(Baby baby, long time) {
        return getBabyDay(baby, baby.getState(), time);
    }

    public static String getBabyDayByDueDateOrLastDate(long time, long time2) {
        DateTime startTime = new DateTime(time);
        DateTime endTime = new DateTime(time2);
        Period pWeek = new Period(startTime, endTime, PeriodType.weeks());
        String week = "";
        String day = "";
        if (pWeek.getWeeks() > 0) {
            week = pWeek.getWeeks() + "周";
        }
        Period pDay = new Period(startTime, endTime, PeriodType.yearWeekDay());
        if (pDay.getDays() > 0) {
            day = pDay.getDays() + "天";
        }
        if (TextUtils.isEmpty(week) && TextUtils.isEmpty(day)) {
            return "孕0天";
        }
        return "孕" + week + day;
    }

    public static String getBabyDayByBirthday(long birthday, long time2) {
        DateTime startTime = new DateTime(birthday);
        DateTime endTime = new DateTime(time2);
        Period p = new Period(startTime, endTime, PeriodType.yearMonthDay());
        String year = "";
        String month = "";
        String day = "";
        if (p.getYears() > 0) {
            year = p.getYears() + "岁";
        }
        if (p.getMonths() > 0) {
            month = p.getMonths() + "个月";
        }
        if (p.getDays() > 0) {
            day = p.getDays() + "天";
        }
        if ((year + month + day).equals("")) {
            return "0天";
        } else {
            return year + month + day;
        }
    }

    public static String getBabyNoteName(Baby baby) {
        return "来自" + " " + baby.getName() + "的成长日记";
    }
}
